var Promise = require('bluebird');
var ObjectID = require('mongodb').ObjectID;
var cheerio = require('cheerio');
var _ = require('lodash');

var mongoBase = require('../common/mongo-base');
var page = require('../common/page');
var utils = require('../common/utils');

module.exports = {
  showChatDay: showChatDay,
  showChatMonth: showChatMonth,
  showChatYear: showChatYear,
  showChatKeyword: showChatKeyword
};


function showChatDay(req, res, next) {
  var num = Math.floor(req.body.num);
  num = num ? num: 10;

  var query = [
    { $group:
      { _id:{year:{
          $year:"$date"}, 
          month:{$month:"$date"}, 
          day:{$dayOfMonth:"$date"}
        }, 
        count:{$sum:1}
      } 
    }
  ];

  var dateArry = utils.getDateArry(num, 'day');

  mongoBase.aggregateDate('news-article', query)

    .then(function(data) {

      var chatDataObj = {};
      _.forEach(data, function(item) {
        chatDataObj[item._id.year + '-' + item._id.month + '-' + item._id.day] = item.count
      });

      var sumCount = 0; 
      _.forEach(dateArry, function(item) {
        if(chatDataObj[item.y]) {
          item.count = chatDataObj[item.y];
        } else {
          item.count = 0;
        }
        sumCount += item.count;
      });

      _.forEach(dateArry, function(item) {
        item.label = item.y,
        item.value = parseFloat(item.count / sumCount).toFixed(2)
      });

      var resData = {
        title: '最新日变化',
        chatTitle: '航空资讯更新数量统计',
        url: '/admin/showChatDay',
        chatData: dateArry,
        num: num
      };

      res.render('admin/data-chat', resData);
    })

    .catch(function(err){
      console.log(err);
    });
}

function showChatMonth(req, res, next) {
  var num = Math.floor(req.body.num);
  num = num ? num: 10;

  var query = [
    { $group:
      { _id:{
          year:{$year:"$date"}, 
          month:{$month:"$date"}
        }, 
        count:{$sum:1}
      } 
    }
  ];

  var dateArry = utils.getDateArry(num, 'month');

  mongoBase.aggregateDate('news-article', query)

    .then(function(data) {

      var chatDataObj = {};
      _.forEach(data, function(item) {
        chatDataObj[item._id.year + '-' + item._id.month] = item.count
      });

      var sumCount = 0; 
      _.forEach(dateArry, function(item) {
        if(chatDataObj[item.y]) {
          item.count = chatDataObj[item.y];
        } else {
          item.count = 0;
        }
        sumCount += item.count;
      });

      _.forEach(dateArry, function(item) {
        item.label = item.y,
        item.value = parseFloat(item.count / sumCount).toFixed(2)
      });

      var resData = {
        title: '最新月变化',
        chatTitle: '航空资讯更新数量统计',
        url: '/admin/showChatMonth',
        chatData: dateArry,
        num: num
      };

      res.render('admin/data-chat', resData);
    })

    .catch(function(err){
      console.log(err);
    });
}

function showChatYear(req, res, next) {
  var num = Math.floor(req.body.num);
  num = num ? num: 10;

  var query = [
    { $group:
      { _id:{
          year:{$year:"$date"}, 
        }, 
        count:{$sum:1}
      } 
    }
  ];

  var dateArry = utils.getDateArry(num, 'year');

  mongoBase.aggregateDate('news-article', query)

    .then(function(data) {

      var chatDataObj = {};
      _.forEach(data, function(item) {
        chatDataObj[item._id.year] = item.count
      });

      var sumCount = 0; 
      _.forEach(dateArry, function(item) {
        if(chatDataObj[item.y]) {
          item.count = chatDataObj[item.y];
        } else {
          item.count = 0;
        }
        sumCount += item.count;
      });

      _.forEach(dateArry, function(item) {
        item.label = item.y,
        item.value = parseFloat(item.count / sumCount).toFixed(2)
      });

      var resData = {
        title: '最新年变化',
        chatTitle: '航空资讯更新数量统计',
        url: '/admin/showChatYear',
        chatData: dateArry,
        num: num
      };

      res.render('admin/data-chat', resData);
    })

    .catch(function(err){
      console.log(err);
    });
}


function showChatKeyword(req, res, next) {
  var num = Math.floor(req.body.num);
  num = num ? num: 10;

  var query = [ 
    {
      $project: {keywords:1}
    }, 
    { 
      $unwind: "$keywords" 
    }, 
    {
      $group: {_id:{keywords:"$keywords"}, 
      count: {$sum:1}
    } 
  }];

  mongoBase.aggregateDate('news-article', query)

    .then(function(data) {

      var chatData = new Array;
      var dataArry = _.sortBy(data, 'count');
      var len = dataArry.length;
      var sumCount = 0; 
      for(var i = num; i--;) {
        chatData[num - i - 1] ={
          y: dataArry[len - i - 1]._id.keywords,
          count: dataArry[len - i - 1].count
        };
        sumCount += dataArry[len - i - 1].count;
      }
      _.forEach(chatData, function(item) {
        item.label = item.y,
        item.value = parseFloat(item.count / sumCount).toFixed(2)
      });

      var resData = {
        title: '关键词统计',
        chatTitle: '航空资讯关键词数量统计',
        url: '/admin/showChatKeyword',
        isKeywords: true,
        chatData: chatData,
        num: num
      };

      res.render('admin/data-chat', resData);
    })

    .catch(function(err){
      console.log(err);
    });
}